package com.zack.gank.gankandroid.main.find.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zack.gank.gankandroid.find.FindDetailActivity;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.find.bean.Gank;
import com.zack.gank.gankandroid.utils.StringUtils;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by Android on 2017/7/14.
 */
public class SelectFindAdapter extends RecyclerView.Adapter<SelectFindAdapter.SelectViewHolder> {
    List<Gank> mList;
    private Context mContext;
    public SelectFindAdapter(Context mContext ,List<Gank> list) {
        this.mContext=mContext;
        this.mList=list;
    }

    @Override
    public SelectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.select_find_adapter,parent,false);
        return new SelectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SelectViewHolder holder, int position) {
        final Gank gank=mList.get(position);
        holder.tvName.setText(gank.who);
        holder.tvTitle.setText(gank.desc);
        holder.tvTime.setText(StringUtils.repleaceLetterTime(gank.createdAt));
        if(gank.images!=null){
            Glide.with(mContext)
                    .load(gank.images.get(0))
                    .into(holder.ivImage);
        }else if(gank.url!=null){
            Glide.with(mContext)
                    .load(gank.url)
                    .into(holder.ivImage);
        }

       /* else {
//            mContext.getString(R.string.mr_imge)
            Glide.with(mContext)
                    .load(mContext.getString(R.string.mr_imge))
                    .into(holder.ivImage);

        }*/

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, FindDetailActivity.class);
                intent.putExtra("gank",gank);
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class SelectViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        TextView tvName;
        TextView tvTime;
        TextView tvTitle;
        ImageView ivImage;
        public SelectViewHolder(View itemView) {
            super(itemView);
            tvName= (TextView) itemView.findViewById(R.id.tv_name);
            tvTime= (TextView) itemView.findViewById(R.id.tv_time);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            ivImage= (ImageView) itemView.findViewById(R.id.iv_img);
            cardView= (CardView) itemView.findViewById(R.id.card_view);
        }
    }

}
